package com.snowflake.ecatalog.service.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.CompanyCategory;
import com.snowflake.ecatalog.repository.CompanyCategoryDao;
import com.snowflake.ecatalog.util.LoginUtil;

@Service
@Transactional(readOnly = true)
public class CompanyCategoryService {
	@Autowired
	private CompanyCategoryDao dao;

	public CompanyCategory getCompanyCategory(Long id) {
		return dao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveCompanyCategory(CompanyCategory newCategory) {
		newCategory.setCreator(LoginUtil.getCurrentUser());
		newCategory.setCreatedTime(new Date());
		newCategory.setStatus(Status.ENABLE);
		dao.save(newCategory);
	}

	@Transactional(readOnly = false)
	public void updateCompanyCategory(CompanyCategory category) {
		category.setLastModifier(LoginUtil.getCurrentUser());
		category.setLastModifiedTime(new Date());
		dao.save(category);
	}

	public List<CompanyCategory> getAllCompanyCategory() {
		return (List<CompanyCategory>) dao.findAll();
	}

	public List<CompanyCategory> getEnabledCompanyCategoryByStd(Long stdId) {
		return dao.findByStdIdAndStatus(stdId, Status.ENABLE);
	}

	@Transactional(readOnly = false)
	public void deleteCompanyCategory(Long id) {
		dao.delete(id);
	}

	public Page<CompanyCategory> getCompanyCategorys(long standardId, Map<String, Object> filterParams, int pageNumber,
			int pageSize) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
		Specification<CompanyCategory> spec = buildSpecification(standardId, filterParams);
		return dao.findAll(spec, pageRequest);
	}

	@Transactional(readOnly = false)
	public void enableCompanyCategoryStandard(Long id) {
		dao.changeStatus(Status.ENABLE, id);
	}

	@Transactional(readOnly = false)
	public void disableCompanyCategoryStandard(Long id) {
		dao.changeStatus(Status.DISABLE, id);
	}

	public boolean existCompanyInCategory(Long categoryId) {
		CompanyCategory category = dao.findOne(categoryId);
		return !CollectionUtils.isEmpty(category.getCompanys());
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
		return new PageRequest(pageNumber - 1, pagzSize);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CompanyCategory> buildSpecification(long standardId, Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if (standardId > 0) {
			filters.put("std.id", new SearchFilter("std.id", Operator.EQ, standardId));
		}
		Specification<CompanyCategory> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				CompanyCategory.class);
		return spec;
	}

}
